<template>
  <view>
    <u-navbar
      v-if="!$isWeixin()"
      :title="title"
      :titleWidth="450"
      :titleBold="true"
      :background="{ backgroundColor: '#ffffff' }"
      :is-fixed="true"
    ></u-navbar>

    <u-tabs active-color="#FF7A23" :list="tabList" :current="tabIndex" @change="index => (tabIndex = index)"></u-tabs>

    <view class="" v-show="tabIndex === 0">
      <u-cell-group>
        <u-cell-item title="协议" :arrow="false" style="padding-bottom: 0;" :border-bottom="false"></u-cell-item>
        <u-cell-item style="padding: 10rpx;" :arrow="false">
          <image-upload @change="onImageChange" v-model="imageList"></image-upload>
        </u-cell-item>
        <u-cell-item style="padding: 10rpx;" :arrow="false">
          <file-picker @change="onFileChange" v-model="fileList"></file-picker>
        </u-cell-item>

        <!-- <u-cell-item title="备注" :arrow="false"><u-input v-model="form.ApplicationRemake" type="textarea" inputAlign="right" placeholder="备注"></u-input></u-cell-item> -->
      </u-cell-group>

      <block v-if="showReview">
        <u-gap height="40" bg-color="#f5f5f5"></u-gap>
        <review-form ref="reviewForm"></review-form>
      </block>
    </view>

    <view class="" v-if="tabIndex === 1"><relations :list="state.This_Dealer_RelationList"></relations></view>

    <view class="" v-if="tabIndex === 2">
      <u-cell-item :arrow="false" title="一级经销商名称" :value="state.First_Dealer_BaseInfo.Name"></u-cell-item>
      <u-cell-item :arrow="false" title="二级经销商名称" :value="state.Double_Dealer_BaseInfo.Name"></u-cell-item>
      <u-cell-item :arrow="false" title="门店名称" :value="state.Door_Dealer_BaseInfo.Name"></u-cell-item>
      <u-cell-item :arrow="false" title="门店代码" :value="state.Door_Dealer_BaseInfo.Account"></u-cell-item>

      <store-renovation-information
        :showPayType="false"
        :showIsExclusive="false"
        :showReview="false"
        :data="{
          EffectList: state.EffectList,
          RealList: state.RealList,
          Price: state.StoreRenovationApplyInfo.Price,
          Account: state.Double_Dealer_BaseInfo.Account
        }"
      ></store-renovation-information>
    </view>

    <view class="btn-group-fixed">
      <u-button v-if="showReview" type="warning" @tap="submit">提交验收审核</u-button>
    </view>

    <u-toast ref="uToast" />
  </view>
</template>

<script>
export default {
  data() {
    return {
      title: '协议验收审核',
      tabList: [{ name: '协议回传' }, { name: '销售业务关系' }, { name: '专卖店费用信息' }],
      tabIndex: 0,

      StoreRenovationId: 1040,

      imageList: [],
      fileList: [],

      showReview: false,

      state: {
        ShowLevel: '',
        First_Dealer_BaseInfo: {},
        Double_Dealer_BaseInfo: {},
        Door_Dealer_BaseInfo: {},
        This_Dealer_OtherInfo: {},
        This_Dealer_RelationList: [],
        StoreRenovationApplyInfo: {},
        EffectList: [],
        RealList: []
      },

      form: {
        AgreementAcceptanceId: '',
        DealerId: '',
        Type: 2,
        AgreementPhotoList: [],
        State: '',
        AuditRemark: ''
      }
    };
  },

  onLoad(options) {
    if (options.Id) {
      this.form.AgreementAcceptanceId = options.Id;
    }
    if (options.DealerId) {
      this.form.DealerId = options.DealerId;
    }
    if (options.ApplyType) {
      this.form.Type = options.ApplyType;

      if (options.ApplyType == 1) {
        this.title = '动力金验收审核';
      }
      if (options.ApplyType == 2) {
        this.title = '渠道补贴验收审核';
      }
    }
    this.init();
    if (this.$isWeixin()) {
      this.$setTitle(this.title);
    }
  },

  methods: {
    onImageChange(list) {
      const initialPhotoList = [];
      if (this.fileList.length > 0) {
        initialPhotoList.push(...this.filesList);
      }
      initialPhotoList.push(...list);
      this.form.AgreementPhotoList = initialPhotoList;
    },

    onFileChange(list) {
      const initialPhotoList = [];
      if (this.imageList.length > 0) {
        initialPhotoList.push(...this.imageList);
      }
      initialPhotoList.push(...list);
      this.form.AgreementPhotoList = initialPhotoList;
    },

    submit() {
      console.log(this.form);

      this.form.AuditRemark = this.$refs.reviewForm.form.AuditRemark;
      this.form.State = this.$refs.reviewForm.form.State;

      console.log(this.form);

      if (!this.form.State) {
        this.$u.toast('请选择审核状态');
        return false;
      }

      this.$u.api.approvalAgreementRenovation(this.form).then(res => {
        const { ResultCode, ResultMessage } = res;
        if (ResultCode === 1) {
          this.$refs.uToast.show({
            title: ResultMessage,
            back: true
          });
        }
      });
    },

    init() {
      this.$u.api
        .getStoreDesignReviewDetail({
          AgreementAcceptanceId: this.form.AgreementAcceptanceId,
          ApplyType: this.form.ApplyType
        })
        .then(res => {
          console.log(res);
          const { ResultCode, ResultData } = res;
          if (ResultCode === 1) {
            if (this.vuex_user.ManagerId == res.NextManagerId) {
              this.showReview = true;
            }

            this.state = ResultData;
            this.state.SR_AgreementInfo.AuditForList = ResultData.AuditForList;

            this.imageList = this.state.AgreementPhotoList.filter(item => item.TypeId == 1);
            this.fileList = this.state.AgreementPhotoList.filter(item => item.TypeId == 4);

            this.form.AgreementPhotoList = this.state.AgreementPhotoList;
          }
        });
    }
  }
};
</script>

<style lang="scss"></style>
